﻿/*************************************************************************
* 计算机名：AA-FA-05
* CLR 版本：4.0.30319.42000
* 命名空间: CustomControls.TreeView
* 类    名：FindVisualChild
* 当 前 域：HYRC
* 创 建 人：dailuyu
* 创建时间：2021/7/7 16:22:14
* 描    述：
*  V0.01 2021 初版
*
*  Copyright (c) 2021 aatsr Corporation. All rights reserved.
*  ┌──────────────────────────────────┐
*  │　此技术信息为本公司机密信息，未经本公司书面同意禁止向第三方披露．　│
*  └──────────────────────────────────┘
***************************************************************************/

using Unity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Media;

namespace CustomControls.TreeView
{
    public class FindVisualChild
    {

        /// <summary>
        /// 关闭所有扩展
        /// </summary>
        public  void CloseAllTreeItemExtend(System.Windows.Controls.TreeView tree) 
        {
            for (var i = 0; i < tree.Items.Count; i++)
            {
                TreeViewItem subContainer = (TreeViewItem)tree.ItemContainerGenerator.ContainerFromIndex(i);
                subContainer.IsExpanded = false;
            }
        }

        /// <summary>
        /// 获取当前节点下符合条件的子元素
        /// </summary>
        /// <param name="container">当前节点</param>
        /// <param name="item">要查询的元素</param>
        /// <returns></returns>
        public  TreeViewItem GetTreeViewItem(ItemsControl container, object item)
        {
            if (container != null)
            {
                //此处是筛选条件 可根据实际情况修改
                if (container.DataContext == item)
                {
                    container.Focus();
                    ((TreeViewItem)container).IsSelected = true;
                    return container as TreeViewItem;
                }
                //Console.WriteLine(((VideoDeviceModel)container.DataContext).Name);
                // 展开当前树节点
                if (container is TreeViewItem && !((TreeViewItem)container).IsExpanded)
                {
                    container.SetValue(TreeViewItem.IsExpandedProperty, true);
                }

                //尝试生成ItemsPresenter和ItemsPanel。
                // 通过调用ApplyTemplate。 请注意，在虚拟化情况下，即使项目已标记
                // 扩展后我们仍然需要这样做去 
                // 重新生成视觉效果，因为它们可能已被虚拟化。
                container.ApplyTemplate();
                ItemsPresenter itemsPresenter =
                    (ItemsPresenter)container.Template.FindName("ItemsHost", container);
                if (itemsPresenter != null)
                {
                    itemsPresenter.ApplyTemplate();
                }
                else
                {
                    // Tree模板尚未命名ItemsPresenter， 
                    // 执行descendents找到子元素.
                    itemsPresenter = VisualHelper.FindVisualChildd<ItemsPresenter>(container);
                    if (itemsPresenter == null)
                    {
                        container.UpdateLayout();

                        itemsPresenter = VisualHelper.FindVisualChildd<ItemsPresenter>(container);
                    }
                }
                Panel itemsHostPanel = (Panel)VisualTreeHelper.GetChild(itemsPresenter, 0);
                // 确保已创建此面板的生成器。
                UIElementCollection children = itemsHostPanel.Children;
                MyVirtualizingStackPanel virtualizingPanel = itemsHostPanel as MyVirtualizingStackPanel;
                for (int i = 0, count = container.Items.Count; i < count; i++)
                {
                    TreeViewItem subContainer;
                    if (virtualizingPanel != null)
                    {
                        // 将项目置于视图中 
                        // 将生成容器。
                        virtualizingPanel.BringIntoView(i);

                        subContainer =
                            (TreeViewItem)container.ItemContainerGenerator.
                            ContainerFromIndex(i);
                    }
                    else
                    {
                        subContainer =
                            (TreeViewItem)container.ItemContainerGenerator.
                            ContainerFromIndex(i);

                        // Bring the item into view to maintain the 
                        // same behavior as with a virtualizing panel.
                        subContainer.BringIntoView();
                    }

                    if (subContainer != null)
                    {
                        // Search the next level for the object.
                        TreeViewItem resultContainer = GetTreeViewItem(subContainer, item);
                        if (resultContainer != null)
                        {
                            return resultContainer;
                        }
                        else
                        {
                            // 元素不在TreeViewItem中
                            // 折叠它
                            subContainer.IsExpanded = false;
                        }
                    }
                }
            }

            return null;
        }
    }

    /// <summary>
    /// 自定义VirtualizingStackPanelBr 用于获取树节点的所有子元素信息（此类随便放到程序能调用的地 
    ///方就行）
    /// </summary>

    public class MyVirtualizingStackPanel : VirtualizingStackPanel
    {
        /// <summary>
        /// Publically expose BringIndexIntoView.
        /// </summary>
        public void BringIntoView(int index)
        {

            this.BringIndexIntoView(index);
        }
    }
}
